PCA Index Interactive Visualization Examples

PCA Index Interactive Visualization Examples#

import plotly.express as px

import config
import load_fred
import pca_index

DATA_DIR = config.DATA_DIR
df = load_fred.load_fred(data_dir=DATA_DIR)
dfn = pca_index.transform_series(df)
/home/runner/work/ryanyuxuanbai_personal_web/ryanyuxuanbai_personal_web/src/pca_index.py:31: FutureWarning:

The default fill_method='pad' in Series.pct_change is deprecated and will be removed in a future version. Either fill in any non-leading NA values prior to calling pct_change or specify 'fill_method=None' to not fill NA values.

/home/runner/work/ryanyuxuanbai_personal_web/ryanyuxuanbai_personal_web/src/pca_index.py:32: FutureWarning:

The default fill_method='pad' in Series.pct_change is deprecated and will be removed in a future version. Either fill in any non-leading NA values prior to calling pct_change or specify 'fill_method=None' to not fill NA values.
print("Hello world")
Hello world
## Visualize Principal Component 1
pc1, loadings = pca_index.pca(dfn, module="scikitlearn")
import matplotlib.pyplot as plt
import numpy as np

# Generating random data for a linear regression model
np.random.seed(0)  # Setting a random seed for reproducibility
x = 2 * np.random.rand(100, 1)  # Random values for x
y = 4 + 3 * x + np.random.randn(100, 1)  # Generating y values as a linear function of x plus some noise

# Plotting the data
plt.figure(figsize=(8, 6))
plt.scatter(x, y)  # Scatter plot of the data points
plt.title("Random Linear Regression Model Data")  # Adding a title
plt.xlabel("X")  # Label for the x-axis
plt.ylabel("Y")  # Label for the y-axis
plt.grid(True)  # Adding a grid for better visualization
plt.show()  # Displaying the plot
../_images/ae6d87ea26105ea831631f3221461caad66c66db8eadd3a20de6fe2be5a039b9.png
# Simple version
fig = px.line(pc1)
fig.show()
# Using slider and quick views
pca_index.pc1_line_plot(pc1)
## Visualize normalized and raw series
dfn.plot(subplots=True, figsize=(10, 10));
../_images/b871589575469d24bd132da0ada06e5b597865d6c030eb8e578ce9166eebed4a.png
fig = px.line(dfn, facet_col="variable", facet_col_wrap=1)
fig.update_yaxes(matches=None)
fig.show()
pca_index.plot_unnormalized_series(df)
pca_index.plot_normalized_series(dfn)